/*******************************************************************************
   @file    buffer.h
   @author  H.M.Shim (elecage@gmail.com)
   @version
   @date    2013. 7. 25.
   @brief
  ******************************************************************************
  The MIT License (MIT)

Copyright (c) 2013 <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
  ******************************************************************************
  */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef BUFFER_H_
#define BUFFER_H_


/* Includes ------------------------------------------------------------------*/
#include "stdint.h"

/* Exported tydpef -----------------------------------------------------------*/

/**
 * @struct _buffer
 * @brief 바이트 단위의 입출력 버퍼
 */
typedef struct _buffer{
	int8_t* buf;		/**< @brief 데이터를 담는 버퍼의 포인터 */
	int32_t	len;		/**< @brief 버퍼의 길이 */
	int32_t	head;		/**< @brief 버퍼에서 데이터를 가져가는 배령 인덱스 */
	int32_t tail;		/**< @brief 버퍼의 끝 */
	uint32_t used;
}__attribute__((packed))BUFFER;

/* Exported constants --------------------------------------------------------*/
/** @brief버퍼 에러. 버퍼가 꽉 차 있을 때 넣기를 시도할 경우 바발생. */
#define BUF_ERR_OVERFLOW		1
/** @brief버퍼 에러. 버퍼가 비어 있을 때 가져가기를 시도할 경우 발생 */
#define BUF_ERR_UNDERFLOW		2
/* Exported macro ------------------------------------------------------------*/

/* Exported variables ------------------------------------------------------- */

/* Exported functions ------------------------------------------------------- */
int32_t bufferInit(BUFFER* buffer, int32_t len);
int32_t bufferDeinit(BUFFER* buffer);
int32_t	bufferRead(BUFFER* buffer, int8_t* data);
int32_t bufferWrite(BUFFER* buffer, int8_t data);
int32_t isBufferEmpty(BUFFER* buffer);
int32_t isBufferFull(BUFFER* buffer);
int32_t getBufferUsedSize(BUFFER* buffer);

#endif /* BUFFER_H_ */
